﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gmis.IDAL;
using System.Data.SqlClient;
using System.Data;
using Gmis.DBUtility;
namespace Gmis.SqlServerDAL
{
    public  class LeaveWord:ILeaveWord
    {
        public LeaveWord()
        { }
     

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Gmis.Model.LeaveWord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into LeaveWord(");
            strSql.Append("Id,Title,contents,phone,contactForm,leaveWordTime,replyTime,replyPerson,replyContents,leaveWordPerson,IsShow)");
            strSql.Append(" values (");
            strSql.Append("@Id,@Title,@contents,@phone,@contactForm,@leaveWordTime,@replyTime,@replyPerson,@replyContents,@leaveWordPerson,@IsShow)");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.VarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@contents", SqlDbType.NText),
					new SqlParameter("@phone", SqlDbType.VarChar,50),
					new SqlParameter("@contactForm", SqlDbType.NVarChar,50),
					new SqlParameter("@leaveWordTime", SqlDbType.DateTime),
					new SqlParameter("@replyTime", SqlDbType.DateTime),
					new SqlParameter("@replyPerson", SqlDbType.NChar,10),
					new SqlParameter("@replyContents", SqlDbType.NChar,10),
					new SqlParameter("@leaveWordPerson", SqlDbType.NVarChar,50),
					new SqlParameter("@IsShow", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.contents;
            parameters[3].Value = model.phone;
            parameters[4].Value = model.contactForm;
            parameters[5].Value = model.leaveWordTime;
            parameters[6].Value = model.replyTime;
            parameters[7].Value = model.replyPerson;
            parameters[8].Value = model.replyContents;
            parameters[9].Value = model.leaveWordPerson;
            parameters[10].Value = model.IsShow;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.LeaveWord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update LeaveWord set ");
            strSql.Append("Title=@Title,");
            strSql.Append("contents=@contents,");
            strSql.Append("phone=@phone,");
            strSql.Append("contactForm=@contactForm,");
            strSql.Append("leaveWordTime=@leaveWordTime,");
            strSql.Append("replyTime=@replyTime,");
            strSql.Append("replyPerson=@replyPerson,");
            strSql.Append("replyContents=@replyContents,");
            strSql.Append("leaveWordPerson=@leaveWordPerson,");
            strSql.Append("IsShow=@IsShow");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.VarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@contents", SqlDbType.NText),
					new SqlParameter("@phone", SqlDbType.VarChar,50),
					new SqlParameter("@contactForm", SqlDbType.NVarChar,50),
					new SqlParameter("@leaveWordTime", SqlDbType.DateTime),
					new SqlParameter("@replyTime", SqlDbType.DateTime),
					new SqlParameter("@replyPerson", SqlDbType.NChar,10),
					new SqlParameter("@replyContents", SqlDbType.NText),
					new SqlParameter("@leaveWordPerson", SqlDbType.NVarChar,50),
					new SqlParameter("@IsShow", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.contents;
            parameters[3].Value = model.phone;
            parameters[4].Value = model.contactForm;
            parameters[5].Value = model.leaveWordTime;
            parameters[6].Value = model.replyTime;
            parameters[7].Value = model.replyPerson;
            parameters[8].Value = model.replyContents;
            parameters[9].Value = model.leaveWordPerson;
            parameters[10].Value = model.IsShow;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Reply(Model.LeaveWord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update LeaveWord set ");
           
            strSql.Append("replyTime=@replyTime,");
            strSql.Append("replyPerson=@replyPerson,");
            strSql.Append("replyContents=@replyContents,");
           
            strSql.Append("IsShow=@IsShow");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.VarChar,50),
					
					new SqlParameter("@replyTime", SqlDbType.DateTime),
					new SqlParameter("@replyPerson", SqlDbType.NChar,10),
					new SqlParameter("@replyContents", SqlDbType.NText),
					
					new SqlParameter("@IsShow", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
           
            parameters[1].Value = model.replyTime;
            parameters[2].Value = model.replyPerson;
            parameters[3].Value = model.replyContents;
           
            parameters[4].Value = model.IsShow;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
 
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LeaveWord ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.VarChar,50)};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LeaveWord ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.LeaveWord GetModel(string Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Title,contents,phone,contactForm,leaveWordTime,replyTime,replyPerson,replyContents,leaveWordPerson,IsShow from LeaveWord ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.VarChar,50)};
            parameters[0].Value = Id;

            Gmis.Model.LeaveWord model = new Gmis.Model.LeaveWord();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.Id = ds.Tables[0].Rows[0]["Id"].ToString();
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.contents = ds.Tables[0].Rows[0]["contents"].ToString();
                model.phone = ds.Tables[0].Rows[0]["phone"].ToString();
                model.contactForm = ds.Tables[0].Rows[0]["contactForm"].ToString();
                if (ds.Tables[0].Rows[0]["leaveWordTime"].ToString() != "")
                {
                    model.leaveWordTime = DateTime.Parse(ds.Tables[0].Rows[0]["leaveWordTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["replyTime"].ToString() != "")
                {
                    model.replyTime = DateTime.Parse(ds.Tables[0].Rows[0]["replyTime"].ToString());
                }
                model.replyPerson = ds.Tables[0].Rows[0]["replyPerson"].ToString();
                model.replyContents = ds.Tables[0].Rows[0]["replyContents"].ToString();
                model.leaveWordPerson = ds.Tables[0].Rows[0]["leaveWordPerson"].ToString();
                if (ds.Tables[0].Rows[0]["IsShow"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsShow"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsShow"].ToString().ToLower() == "true"))
                    {
                        model.IsShow = true;
                    }
                    else
                    {
                        model.IsShow = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Title,contents,phone,contactForm,leaveWordTime,replyTime,replyPerson,replyContents,leaveWordPerson,IsShow ");
            strSql.Append(" FROM LeaveWord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,Title,contents,phone,contactForm,leaveWordTime,replyTime,replyPerson,replyContents,leaveWordPerson,IsShow ");
            strSql.Append(" FROM LeaveWord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
